import * as COS from 'cos-nodejs-sdk-v5';
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { TencentBucket_UploadFile_SeClass } from './assets/tencentBucket.class';

@Injectable()
export class TencentBucket_Service {
  private cos: COS;
  constructor(private configService: ConfigService) {
    this.cos = new COS({
      SecretId: this.configService.get<string>('COS_SECRETID'),
      SecretKey: this.configService.get<string>('COS_SECRETKEY'),
    });
  }

  async tencentBucket_UploadFile(obj: TencentBucket_UploadFile_SeClass) {
    const bucket = obj.bucket || this.configService.get<string>('COS_BUCKET');
    const key = new Date().getTime() + '-' + bucket + '-' + obj.fileName;
    await this.cos.putObject({
      Bucket: bucket,
      Region: obj.region || this.configService.get<string>('COS_REGION'),
      Key: key,
      Body: obj.fileContent,
    });
    return this.configService.get<string>('COS_URL') + key;
  }
}
